package fastjsonDemo.JSONDemo.AnnotationDemo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import org.junit.Test;

import java.util.Date;

public class JSONFieldDemo {


	static Pojo pojo;

	static {
		pojo = new Pojo();
		pojo.setId(1).setDate1(new Date()).setDate2(new Date()).setDate3(new Date()).setF1(111).setF2(222);


	}

	@Test
	public void ceui() {
		String jsonString = JSON.toJSONString(pojo);
		System.out.println("jsonString = " + jsonString);
		//结果: {"date1":"20190830","date3":1567136117221,"pojoId":1,"f2":222,"f1":111}

	}


	static class Pojo {

		//重新命名
		@JSONField(name = "pojoId")
		private int id;
		// 配置date序列化和反序列使用yyyyMMdd日期格式
		@JSONField(format = "yyyyMMdd")
		private Date date1;

		// 不序列化
		@JSONField(serialize = false)
		private Date date2;

		// 不反序列化
		@JSONField(deserialize = false)
		private Date date3;

		// 按ordinal排序
		@JSONField(ordinal = 2)
		private int f1;

		@JSONField(ordinal = 1)
		private int f2;
		//*****************


		public int getId() {
			return id;
		}

		public Pojo setId(int id) {
			this.id = id;
			return this;
		}

		public Date getDate1() {
			return date1;
		}

		public Pojo setDate1(Date date1) {
			this.date1 = date1;
			return this;
		}

		public Date getDate2() {
			return date2;
		}

		public Pojo setDate2(Date date2) {
			this.date2 = date2;
			return this;
		}

		public Date getDate3() {
			return date3;
		}

		public Pojo setDate3(Date date3) {
			this.date3 = date3;
			return this;
		}

		public int getF1() {
			return f1;
		}

		public Pojo setF1(int f1) {
			this.f1 = f1;
			return this;

		}

		public int getF2() {
			return f2;
		}

		public Pojo setF2(int f2) {
			this.f2 = f2;
			return this;
		}
	}


}
